package cn.ekuma.android.ui.scan.history.upload;

import java.text.SimpleDateFormat;
import java.util.ArrayList;

import android.app.Activity;
import android.content.res.Resources;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
import cn.ekuma.epos.ui.android.R;

import com.openbravo.bean.pda.LocalHistory;

public class LocalHistoryAdapter extends ArrayAdapter<LocalHistory> {

	public final static SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd  HH:mm");
	
	  private final Activity activity;

	  LocalHistoryAdapter(Activity activity) {
	    super(activity, R.layout.history_list_item, new ArrayList<LocalHistory>());
	    this.activity = activity;
	  }

	  @Override
	  public View getView(int position, View view, ViewGroup viewGroup) {
	    LinearLayout layout;
	    if (view instanceof LinearLayout) {
	      layout = (LinearLayout) view;
	    } else {
	      LayoutInflater factory = LayoutInflater.from(activity);
	      layout = (LinearLayout) factory.inflate(R.layout.history_list_item, viewGroup, false);
	    }
	    LocalHistory item = getItem(position);
	    
	    String title;
	    String detail;
	    if (item.getId() != null) {
	      title = dateFormat.format(item.getCreateDate());
	      detail = "";      
	    } else {
	      Resources resources = getContext().getResources();
	      title = resources.getString(R.string.history_empty);
	      detail = resources.getString(R.string.history_empty_detail);
	    }
	    ((TextView) layout.findViewById(R.id.history_title)).setText(title);    
	    ((TextView) layout.findViewById(R.id.history_detail)).setText(detail);
	    return layout;
	  }

	}
